package com.ymatou.iapi.fundcommission.testcase;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.fundcommission.service.DetailReportGetCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.database.sqlwapper.YmtFundDimensionWapper;
import com.ymttest.utils.JsonBeanHelper;
/**
*佣金对账明细查询接口
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_DetailReportGet{
	private static DetailReportGetCall detailreportgetCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("佣金对账明细查询接口");
	}

	@Before
	public void caseUp() {
		detailreportgetCall=new DetailReportGetCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(detailreportgetCall.getOpurl());
	}
	
	public static void detailreportgetreturncode() throws Exception {
	    try {	    	
			//获取返回报文的数据
			String dataStr = detailreportgetCall.getReturnData();
			//返回报文断言
			Logger.verifyNotNull(dataStr, "返回报文非空");			
			String Success =  new JSONObject(dataStr).getString("Success");
			Logger.verifyIsContains(true, Success, "\"Success\":true");
			String errMsg =  new JSONObject(dataStr).getString("ErrMsg");
			Logger.verifyIsNull(errMsg, "Msg等于空");
			String errCode =  new JSONObject(dataStr).getString("ErrCode");
			Logger.verifyIsNull(errCode, "ErrCode等于空");
			String body =  new JSONObject(dataStr).getString("Body");
			Logger.verifyIsNull(body, "Body等于空");
			String requestUrl =  new JSONObject(dataStr).getString("RequestUrl");
			Logger.verifyIsNull(requestUrl, "RequestUrl等于空");
	    	    	    	
	    }catch (Exception e) {  
	        e.printStackTrace();
	    }  
	}
	
	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DetailReportGet_001() {
		Logger.start(true,"佣金对账明细查询(收佣)");
		try {
			//TODO 补全case代码
			YmtFundDimensionWapper ymtFundDimensionWapper = new YmtFundDimensionWapper();
			//取出查询条件数据（Ymt_CommissionCheckBills）
			List<Map> commissionCheckBillslist = ymtFundDimensionWapper.commissionCheckBillsbyBusinessType(11);//11收佣 12返佣 21退佣
			String businessDatebysql = (String) commissionCheckBillslist.get(0).get("BusinessTime");
			Integer selleridbysql = (Integer) commissionCheckBillslist.get(0).get("SellerId");
			String sellerLoginidbysql = (String) commissionCheckBillslist.get(0).get("SellerLoginId");
						
			Map<String,Object> aa = new HashMap<String, Object>();
			aa.put("CommissionType", 1); 	//1收佣 2退佣 3返佣
			aa.put("BeginTime", businessDatebysql);		
			aa.put("EndTime", businessDatebysql);		
			aa.put("UserId", selleridbysql);	
			aa.put("UserLoginId", sellerLoginidbysql);			
			//调用接口json文件
			JSONObject detailreportgetBean =  JsonBeanHelper.getJsonBean("/com.ymatou.iapi.fundcommission/DetailReportGet.json");
			detailreportgetBean = JsonBeanHelper.updateJsonBean(detailreportgetBean, aa);				
			detailreportgetCall.setData(detailreportgetBean);
			detailreportgetCall.callService();			
			detailreportgetreturncode();
			String dataStr = detailreportgetCall.getReturnData();
			//取出报文数据
			int userId = new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getInt("UserId");
			String userLoginId = new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getString("UserLoginId");
			int invoiceType = new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getInt("InvoiceType");	//1订单 2退货退款单
			String invoiceId = new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getString("InvoiceId");	//单据号	
			int commissionType = new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getInt("CommissionType") ; //结算类型 1扣佣 2退佣 3返佣
			BigDecimal commissionAmount = new BigDecimal((new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getDouble("CommissionAmount"))).setScale(2, BigDecimal.ROUND_HALF_UP); //佣金结算金额	
			String settlementTime = new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getString("SettlementTime"); 	//结算时间
			settlementTime = settlementTime.substring(0, settlementTime.indexOf('T')); //截取到T为止的字符
	        if("0001-01-01".equals(settlementTime))
        	{
	        	settlementTime = "null";		//数据库内日期为null值，接口返回"0001-01-01"
        	}
			String sourceOrderId = new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getString("SourceOrderId"); //原订单号
			boolean newSellerFlag = "1".equals(new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getString("NewSellerFlag")); //是否新买手			
			boolean newCustFlag = "1".equals(new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getString("NewCustFlag"));	//是否新客户
			//SQL取出断言数据（Ymt_CommissionCheckBills）
			Map m = new HashMap();
	    	m.put("businessType", 11);
	    	m.put("invoiceId", invoiceId);	    	
	        List<Map> commissionCheckBillsbyOrderIdlist = ymtFundDimensionWapper.commissionCheckBillsbyOrderId(m);
	        Short invoiceTypebysqlshort = (Short) commissionCheckBillsbyOrderIdlist.get(0).get("InvoiceType"); //1订单 2退货退款单
	        	int invoiceTypebysql = (Short)invoiceTypebysqlshort;
	        String invoiceIdBysql = (String) commissionCheckBillsbyOrderIdlist.get(0).get("InvoiceId"); //单据号	
	        BigDecimal commissionBysql = ((BigDecimal) commissionCheckBillsbyOrderIdlist.get(0).get("Commission")).setScale(2, BigDecimal.ROUND_HALF_UP);
	        String oriBillIdBysql = (String) commissionCheckBillsbyOrderIdlist.get(0).get("OriBillId");
	        String settlementDateBysql = (String) commissionCheckBillsbyOrderIdlist.get(0).get("SettlementTime");
	        if(settlementDateBysql == null)
        	{
	        	settlementDateBysql = "null";	//null无法比较，转换为字符串null
        	}		        
	        Boolean newSellerFlagBysql = (Boolean) commissionCheckBillsbyOrderIdlist.get(0).get("NewSellerFlag");
	        Boolean newCustFlagBysql = (Boolean) commissionCheckBillsbyOrderIdlist.get(0).get("NewCustFlag");     
	        //断言
	        Logger.verifyEquals(selleridbysql, userId, "用户id对比");
	        Logger.verifyEquals(sellerLoginidbysql, userLoginId, "用户名对比");
	        Logger.verifyEquals(invoiceTypebysql, invoiceType, "单据类型对比");
	        Logger.verifyEquals(invoiceIdBysql, invoiceId, "单据号对比");
	        Logger.verifyEquals(1, commissionType, "结算对比");
	        Logger.verifyEquals(commissionBysql, commissionAmount, "结算佣金金额对比");
	        Logger.verifyEquals(settlementDateBysql, settlementTime, "结算日期对比");
	        Logger.verifyEquals(oriBillIdBysql, sourceOrderId, "原订单号对比");
	        Logger.verifyEquals(newSellerFlagBysql, newSellerFlag, "是否新买手对比");
	        Logger.verifyEquals(newCustFlagBysql, newCustFlag, "新否新客对比");       
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DetailReportGet_002() {
		Logger.start(true,"佣金对账明细查询(退佣)");
		try {
			//TODO 补全case代码
			YmtFundDimensionWapper ymtFundDimensionWapper = new YmtFundDimensionWapper();
			//取出查询条件数据（Ymt_CommissionCheckBills）
			List<Map> commissionCheckBillslist = ymtFundDimensionWapper.commissionCheckBillsbyBusinessType(21);//11收佣 12返佣 21退佣
			String businessDatebysql = (String) commissionCheckBillslist.get(0).get("BusinessTime");
			Integer selleridbysql = (Integer) commissionCheckBillslist.get(0).get("SellerId");
			String sellerLoginidbysql = (String) commissionCheckBillslist.get(0).get("SellerLoginId");
						
			Map<String,Object> aa = new HashMap<String, Object>();
			aa.put("CommissionType", 2); 	//1收佣 2退佣 3返佣
			aa.put("BeginTime", businessDatebysql);		
			aa.put("EndTime", businessDatebysql);		
			aa.put("UserId", selleridbysql);	
			aa.put("UserLoginId", sellerLoginidbysql);			
			//调用接口json文件
			JSONObject detailreportgetBean =  JsonBeanHelper.getJsonBean("/com.ymatou.iapi.fundcommission/DetailReportGet.json");
			detailreportgetBean = JsonBeanHelper.updateJsonBean(detailreportgetBean, aa);				
			detailreportgetCall.setData(detailreportgetBean);
			detailreportgetCall.callService();			
			detailreportgetreturncode();
			String dataStr = detailreportgetCall.getReturnData();
			//取出报文数据
			int userId = new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getInt("UserId");
			String userLoginId = new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getString("UserLoginId");
			int invoiceType = new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getInt("InvoiceType");	//1订单 2退货退款单
			String invoiceId = new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getString("InvoiceId");	//单据号	
			int commissionType = new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getInt("CommissionType") ; //结算类型 1扣佣 2退佣 3返佣
			BigDecimal commissionAmount = new BigDecimal((new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getDouble("CommissionAmount"))).setScale(2, BigDecimal.ROUND_HALF_UP); //佣金结算金额	
			String settlementTime = new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getString("SettlementTime"); 	//结算时间
			settlementTime = settlementTime.substring(0, settlementTime.indexOf('T')); //截取到T为止的字符
	        if("0001-01-01".equals(settlementTime))
        	{
	        	settlementTime = "null";		//数据库内日期为null值，接口返回"0001-01-01"
        	}
			String sourceOrderId = new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getString("SourceOrderId"); //原订单号
			boolean newSellerFlag = "1".equals(new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getString("NewSellerFlag")); //是否新买手			
			boolean newCustFlag = "1".equals(new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getString("NewCustFlag"));	//是否新客户
			//SQL取出断言数据（Ymt_CommissionCheckBills）
			Map m = new HashMap();
	    	m.put("businessType", 21);
	    	m.put("invoiceId", invoiceId);	    	
	        List<Map> commissionCheckBillsbyOrderIdlist = ymtFundDimensionWapper.commissionCheckBillsbyOrderId(m);
	        Short invoiceTypebysqlshort = (Short) commissionCheckBillsbyOrderIdlist.get(0).get("InvoiceType"); //1订单 2退货退款单
	        	int invoiceTypebysql = (Short)invoiceTypebysqlshort;
	        String invoiceIdBysql = (String) commissionCheckBillsbyOrderIdlist.get(0).get("InvoiceId"); //单据号	
	        BigDecimal commissionBysql = ((BigDecimal) commissionCheckBillsbyOrderIdlist.get(0).get("Commission")).setScale(2, BigDecimal.ROUND_HALF_UP);
	        String oriBillIdBysql = (String) commissionCheckBillsbyOrderIdlist.get(0).get("OriBillId");
	        String settlementDateBysql = (String) commissionCheckBillsbyOrderIdlist.get(0).get("SettlementTime");
	        if(settlementDateBysql == null)
        	{
	        	settlementDateBysql = "null";	//null无法比较，转换为字符串null
        	}	
	        Boolean newSellerFlagBysql = (Boolean) commissionCheckBillsbyOrderIdlist.get(0).get("NewSellerFlag");
	        Boolean newCustFlagBysql = (Boolean) commissionCheckBillsbyOrderIdlist.get(0).get("NewCustFlag");     
	        //断言
	        Logger.verifyEquals(selleridbysql, userId, "用户id对比");
	        Logger.verifyEquals(sellerLoginidbysql, userLoginId, "用户名对比");
	        Logger.verifyEquals(invoiceTypebysql, invoiceType, "单据类型对比");
	        Logger.verifyEquals(invoiceIdBysql, invoiceId, "单据号对比");
	        Logger.verifyEquals(2, commissionType, "结算对比");
	        Logger.verifyEquals(commissionBysql, commissionAmount, "结算佣金金额对比");
	        Logger.verifyEquals(settlementDateBysql, settlementTime, "结算日期对比");
	        Logger.verifyEquals(oriBillIdBysql, sourceOrderId, "原订单号对比");
	        Logger.verifyEquals(newSellerFlagBysql, newSellerFlag, "是否新买手对比");
	        Logger.verifyEquals(newCustFlagBysql, newCustFlag, "新否新客对比");       
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DetailReportGet_003() {
		Logger.start(true,"佣金对账明细查询(返佣)");
		try {
			//TODO 补全case代码
			YmtFundDimensionWapper ymtFundDimensionWapper = new YmtFundDimensionWapper();
			//取出查询条件数据（Ymt_CommissionCheckBills）
			List<Map> commissionCheckBillslist = ymtFundDimensionWapper.commissionCheckBillsbyBusinessType(12);//11收佣 12返佣 21退佣
			String businessDatebysql = (String) commissionCheckBillslist.get(0).get("BusinessTime");
			Integer selleridbysql = (Integer) commissionCheckBillslist.get(0).get("SellerId");
			String sellerLoginidbysql = (String) commissionCheckBillslist.get(0).get("SellerLoginId");
						
			Map<String,Object> aa = new HashMap<String, Object>();
			aa.put("CommissionType", 3); 	//1收佣 2退佣 3返佣
			aa.put("BeginTime", businessDatebysql);		
			aa.put("EndTime", businessDatebysql);		
			aa.put("UserId", selleridbysql);	
			aa.put("UserLoginId", sellerLoginidbysql);			
			//调用接口json文件
			JSONObject detailreportgetBean =  JsonBeanHelper.getJsonBean("/com.ymatou.iapi.fundcommission/DetailReportGet.json");
			detailreportgetBean = JsonBeanHelper.updateJsonBean(detailreportgetBean, aa);				
			detailreportgetCall.setData(detailreportgetBean);
			detailreportgetCall.callService();			
			detailreportgetreturncode();
			String dataStr = detailreportgetCall.getReturnData();
			//取出报文数据
			int userId = new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getInt("UserId");
			String userLoginId = new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getString("UserLoginId");
			int invoiceType = new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getInt("InvoiceType");	//1订单 2退货退款单
			String invoiceId = new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getString("InvoiceId");	//单据号	
			int commissionType = new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getInt("CommissionType") ; //结算类型 1扣佣 2退佣 3返佣
			BigDecimal commissionAmount = new BigDecimal((new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getDouble("CommissionAmount"))).setScale(2, BigDecimal.ROUND_HALF_UP); //佣金结算金额	
			String settlementTime = new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getString("SettlementTime"); 	//结算时间
			settlementTime = settlementTime.substring(0, settlementTime.indexOf('T')); //截取到T为止的字符
	        if("0001-01-01".equals(settlementTime))
        	{
	        	settlementTime = "null";		//数据库内日期为null值，接口返回"0001-01-01"
        	}
			String sourceOrderId = new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getString("SourceOrderId"); //原订单号
			boolean newSellerFlag = "1".equals(new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getString("NewSellerFlag")); //是否新买手			
			boolean newCustFlag = "1".equals(new JSONObject(dataStr).getJSONArray("DetailReportList").getJSONObject(0).getString("NewCustFlag"));	//是否新客户
			//SQL取出断言数据（Ymt_CommissionCheckBills）
			Map m = new HashMap();
	    	m.put("businessType", 12);
	    	m.put("invoiceId", invoiceId);	    	
	        List<Map> commissionCheckBillsbyOrderIdlist = ymtFundDimensionWapper.commissionCheckBillsbyOrderId(m);
	        Short invoiceTypebysqlshort = (Short) commissionCheckBillsbyOrderIdlist.get(0).get("InvoiceType"); //1订单 2退货退款单
	        	int invoiceTypebysql = (Short)invoiceTypebysqlshort;
	        String invoiceIdBysql = (String) commissionCheckBillsbyOrderIdlist.get(0).get("InvoiceId"); //单据号	
	        BigDecimal commissionBysql = ((BigDecimal) commissionCheckBillsbyOrderIdlist.get(0).get("Commission")).setScale(2, BigDecimal.ROUND_HALF_UP);
	        String oriBillIdBysql = (String) commissionCheckBillsbyOrderIdlist.get(0).get("OriBillId");
	        String settlementDateBysql = (String) commissionCheckBillsbyOrderIdlist.get(0).get("SettlementTime");
	        if(settlementDateBysql == null)
        	{
	        	settlementDateBysql = "null";	//null无法比较，转换为字符串null
        	}	
	        Boolean newSellerFlagBysql = (Boolean) commissionCheckBillsbyOrderIdlist.get(0).get("NewSellerFlag");
	        Boolean newCustFlagBysql = (Boolean) commissionCheckBillsbyOrderIdlist.get(0).get("NewCustFlag");     
	        //断言
	        Logger.verifyEquals(selleridbysql, userId, "用户id对比");
	        Logger.verifyEquals(sellerLoginidbysql, userLoginId, "用户名对比");
	        Logger.verifyEquals(invoiceTypebysql, invoiceType, "单据类型对比");
	        Logger.verifyEquals(invoiceIdBysql, invoiceId, "单据号对比");
	        Logger.verifyEquals(3, commissionType, "结算对比");
	        Logger.verifyEquals(commissionBysql, commissionAmount, "结算佣金金额对比");
	        Logger.verifyEquals(settlementDateBysql, settlementTime, "结算日期对比");
	        Logger.verifyEquals(oriBillIdBysql, sourceOrderId, "原订单号对比");
	        Logger.verifyEquals(newSellerFlagBysql, newSellerFlag, "是否新买手对比");
	        Logger.verifyEquals(newCustFlagBysql, newCustFlag, "新否新客对比");       
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_DetailReportGet_004() {
		Logger.start(true,"分页开关");
		try {
			//TODO 补全case代码						
			Map<String,Object> aa = new HashMap<String, Object>();
			aa.put("CommissionType", 3); 	//1收佣 2退佣 3返佣
			aa.put("BeginTime", "2014-01-01");		
			aa.put("EndTime", "2020-01-01");		
			aa.put("UserId", null);	
			aa.put("UserLoginId", null);
			aa.put("PageSize", 2);
			aa.put("IsPaged", true);
			//调用接口json文件
			JSONObject detailreportgetBean =  JsonBeanHelper.getJsonBean("/com.ymatou.iapi.fundcommission/DetailReportGet.json");
			detailreportgetBean = JsonBeanHelper.updateJsonBean(detailreportgetBean, aa);				
			detailreportgetCall.setData(detailreportgetBean);
			detailreportgetCall.callService();			
			detailreportgetreturncode();
			String dataStr = detailreportgetCall.getReturnData();			
			int detailReportListength = new JSONObject(dataStr).getJSONArray("DetailReportList").length();
			Logger.verifyEquals(2, detailReportListength, "每页显示2条");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
}